#include <lib_acl.h>



ACL_ARRAY *read_file(const char *filename, ACL_ARRAY *array);

int main(int argc, char **argv) {

    const char *filename = "/home/chongzi/go/t.ini";
    ACL_ARRAY *array = acl_array_create(16);
    if (read_file(filename, array) == NULL) {
        acl_msg_error("read file error:%s\n", acl_last_serror());
        return EXIT_FAILURE;
    }
    
    ACL_ITER iter;
    ACL_FOREACH(iter, array) {
       puts((char *)iter.data);
    }
    return EXIT_SUCCESS;
}

/**
 * read file
 *
 * @param const char * filename
 * @param ACL_ARRAY *  array
 *
 * @retrun ACL_ARRAY * array
 */
ACL_ARRAY *read_file(const char *filename, ACL_ARRAY *array) {
    if (filename == NULL || array == NULL || *filename == '\0') {
        return NULL;
    }
    char buf[1024];
    const char *func_name = "read_file";
    ACL_VSTRING *str = acl_vstring_alloc(1024);
    ACL_FILE *fp = acl_fopen(filename, "r");
    if (fp == NULL) {
        return NULL;
    }
    while (!acl_feof(fp)) {
        if (acl_fgets(buf, 1024, fp) != NULL) {
            acl_vstring_memcat(str, buf, strlen(buf));
            if (strchr(buf, '\n') != NULL) {
                acl_array_append(array, acl_strdup_glue(func_name, __LINE__, acl_vstring_str(str)));
                ACL_VSTRING_RESET(str);
            }
        }
    }

    acl_fclose(fp);
    acl_clearerr(fp);
    acl_vstring_free(str);
    return array;
}